package com.vstudy.zx.controller;

import com.vstudy.zx.entity.School;
import com.vstudy.zx.entity.Teacher;
import com.vstudy.zx.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info(Model model) {
        List<School> schools = teacherService.getSchools();
        model.addAttribute("schools", schools);
        return "zx/teacher/info.jsp";
    }

    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    @ResponseBody
    public Object updatePwd(String pwd, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        teacher.setTe_password(pwd.trim());
        int rs = teacherService.updatePwd(teacher);
        if (rs > 0) {
            return 1;
        }
        return 0;
    }

    @RequestMapping(value = "/checkPwd", method = RequestMethod.POST)
    @ResponseBody
    public Object checkPwd(String pwd, HttpServletRequest request) {
        Map<String, Boolean> result = new HashMap<>();
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        if (teacher == null) {
            result.put("valid", false);
        }
        pwd = pwd.trim();
        if (pwd.equals(teacher.getTe_password())) {
            result.put("valid", true);
        } else {
            result.put("valid", false);
        }
        return result;
    }

    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    @ResponseBody
    public Object uploadHeadImg(HttpServletRequest request, MultipartFile img) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        Object result = null;
        try {
            result = teacherService.updateImg(teacher, img, request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "/checkLoginName", method = RequestMethod.POST)
    @ResponseBody
    public Object checkLoginName(String te_login_name, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        Boolean exists = teacherService.exists(te_login_name, teacher.getTe_id());
        Map<String, Boolean> result = new HashMap<>();
        result.put("valid", !exists);
        return result;
    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object updateInfo(Teacher teacher, HttpServletRequest request) {
        Teacher te = (Teacher) request.getSession().getAttribute("teacher");
        teacher.setTe_id(te.getTe_id());
        int rs = teacherService.update(teacher);
        return rs;
    }


}
